package net.chrone.quickpayapi.service.impl;

import java.util.Date;
import java.util.List;

import net.chrone.quickpayapi.api.CiticApi;
import net.chrone.quickpayapi.mapper.OrderMapper;
import net.chrone.quickpayapi.model.CiticWxBaseBean;
import net.chrone.quickpayapi.model.Merchant;
import net.chrone.quickpayapi.model.Order;
import net.chrone.quickpayapi.model.OrderExample;
import net.chrone.quickpayapi.model.OrderExample.Criteria;
import net.chrone.quickpayapi.service.NotifyLogService;
import net.chrone.quickpayapi.service.OrderService;
import net.chrone.quickpayapi.service.PayforService;
import net.chrone.quickpayapi.service.RedisService;
import net.chrone.quickpayapi.util.Constant;
import net.chrone.quickpayapi.util.Object2Xml;
import net.chrone.quickpayapi.util.SdkUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
	
	private  final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private NotifyLogService notifyLogService;
	@Autowired
	private RedisService redisService;
	@Autowired
	private PayforService payforService;

	@Override
	public Order newOrder(Order order,Merchant merchant) throws Exception {
		logger.debug("订单入库");
		DateTime dateTime = new DateTime();
		String orderNo = dateTime.getMillis()+redisService.getIdFromRedis("orderId");
		order.setOrderDt(dateTime.toString("yyyyMMdd"));
		order.setCreated(dateTime.toDate());
		order.setTimeExpire(dateTime.plusDays(1).toDate());
		order.setBankTransactionId(orderNo);
		order.setOrderNo(orderNo);
		int rows = orderMapper.insertSelective(order);
		if(rows==1){
			return order;
		}
		return null;
	}

	@Override
	public Order find(String orgId, String orgOrderNo) {
		OrderExample example = new OrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrgIdEqualTo(orgId);
		criteria.andOrgOrderNoEqualTo(orgOrderNo);
		List<Order> orders = orderMapper.selectByExample(example);
		if(orders!=null&&orders.size()>0){
			return orders.get(0);
		}
		return null;
	}

	@Override
	public Order findByChannelId(String payChannelId,String orderId) {
		OrderExample example = new OrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andPayChannelIdEqualTo(payChannelId);
		criteria.andBankTransactionIdEqualTo(orderId);
		List<Order> orders = orderMapper.selectByExample(example);
		if(orders!=null&&orders.size()>0){
			return orders.get(0);
		}
		return null;
	}


	

	@Override
	public Order findByPrimaryKey(String orderNo) {
		return orderMapper.selectByPrimaryKey(orderNo);
	}

	@Override
	public int updateByPrimaryKey(Order order) {
		return orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public int updateByCiticNotify(Order orgiOrder, CiticWxBaseBean notifyBean) {
		orgiOrder.setModified(new Date());//修改时间
		if(CiticApi.SUCCESS_CODE.equals(notifyBean.getPay_result())){
			orgiOrder.setPaySt(Constant.PAY_ST_PAID_SUCCESS);
			orgiOrder.setTimePaid(new Date());
			orgiOrder.setBankTransactionId(notifyBean.getTransaction_id());
		}else{
			orgiOrder.setBankRspCd(notifyBean.getErr_code());
			orgiOrder.setBankRspDesc(notifyBean.getErr_msg());
			orgiOrder.setPaySt(Constant.PAY_ST_PAID_FAIL);
		}
		int row = orderMapper.updateByPrimaryKeySelective(orgiOrder);//修改原订单
		//T0交易插入付款
		if(Constant.TRAN_TP_0==orgiOrder.getTranTp()){
			payforService.newFromOrder(orgiOrder, Constant.PAYFOR_ST_NEW, null, null);
		}
		if(row==1){
			notifyLogService.insertNotifyLog(orgiOrder.getPayChannelId(), "self", Object2Xml.object2xml(notifyBean), orgiOrder.getOrderNo());
		}
		if(StringUtils.isNotEmpty(orgiOrder.getNotifyUrl())){
//			SdkUtil.notify(orgiOrder);//通知商户
		}
		return row;
	}

}
